package cn.com.fattiger.framework.remoting.netty4.client.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

import cn.com.fattiger.framework.remoting.netty4.common.Invocation;

public class ClientDecoder extends ByteToMessageDecoder {
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		// ByteBufToBytes read = new ByteBufToBytes();
		// Object obj = ByteObjConverter.byteToObject(read.read(in));
		try {
			Long uuid = in.readLong();
			byte[] data = new byte[in.readableBytes()];
			in.readBytes(data);
			String response = new String(data, "utf-8");
			Invocation invocation = new Invocation(uuid);
			invocation.setResponse(response);
			out.add(invocation);
		} finally {
			//ReferenceCountUtil.release(in);
		}
	}

}